package com.wingscode.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.R;
import com.wingscode.common.validator.ValidatorUtils;
import com.wingscode.common.validator.group.AddGroup;
import com.wingscode.common.validator.group.UpdateGroup;
import com.wingscode.modules.common.entity.AddedServiceEntity;
import com.wingscode.modules.common.entity.AddedSettingEntity;
import com.wingscode.modules.common.entity.AddedSettingPriceEntity;
import com.wingscode.modules.common.entity.UserOrderEntity;
import com.wingscode.modules.common.service.*;
import com.wingscode.modules.sys.entity.DictionaryKeyEntity;
import com.wingscode.modules.sys.entity.DictionaryValueEntity;
import com.wingscode.modules.sys.form.ServiceQueryForm;
import com.wingscode.modules.sys.service.DictionaryKeyService;
import com.wingscode.modules.sys.service.DictionaryValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 增值服务
 *
 * @author yan
 */
@RestController
@RequestMapping("/addedService")
public class AddedServiceController extends AbstractController {
	@Autowired
	private ServiceUsageRecordService serviceUsageRecordService;
	@Autowired
	private DictionaryValueService dictionaryValueService;
	@Autowired
	private DictionaryKeyService dictionaryKeyService;
	@Autowired
	private AddedServiceService addedServiceService;
	@Autowired
	private AddedSettingService addedSettingService;
	@Autowired
	private AddedSettingPriceService addedSettingPriceService;
	@Autowired
	private UserOrderService userOrderService;

	
	/**
	 * 增值服务类型列表
	 */
	@GetMapping("/typeList")
	public R typeList(@RequestParam Map<String, Object> params){
		params.put("type", "serviceType");

		PageUtils page = dictionaryValueService.queryByType(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 增值服务类型详情
	 */
	@RequestMapping("/typeInfo/{id}")
	public R typeInfo(@PathVariable("id") Long id){
		DictionaryValueEntity entity = dictionaryValueService.getById(id);

        return R.ok().put("info", entity);
	}
	
	/**
	 * 保存增值服务类型
	 */
	@PostMapping("/typeSave")
	public R typeSave(@RequestBody DictionaryValueEntity entity){
		DictionaryKeyEntity keyEntity = dictionaryKeyService.getOne(
				new QueryWrapper<DictionaryKeyEntity>()
						.eq("type", "serviceType")
		);
		if (keyEntity != null)
			entity.setPid(keyEntity.getId());

		ValidatorUtils.validateEntity(entity, AddGroup.class);

		dictionaryValueService.save(entity);

		return R.ok();
	}

	/**
	 * 修改增值服务类型
	 */
	@PostMapping("/typeUpdate")
	public R typeUpdate(@RequestBody DictionaryValueEntity entity){

		ValidatorUtils.validateEntity(entity, UpdateGroup.class);

		dictionaryValueService.updateById(entity);

		return R.ok();
	}

	/**
	 * 增值服务使用记录列表
	 */
	@PostMapping("/recordList")
	public R recordList(@RequestBody ServiceQueryForm form){
		if (form.getLimit().isEmpty())
			return R.error("limit为空");
		if (form.getPage().isEmpty())
			return R.error("page为空");

		PageUtils page = serviceUsageRecordService.queryPage(form);

		return R.ok().put("page", page);
	}

	/**
	 * 新增增值服务
	 */
	@PostMapping("/addedSave")
	public R addedSave(@RequestBody AddedServiceEntity entity){
		if (entity.getName().isEmpty())
			return R.error("name为空");
		if (entity.getSettingList() == null || entity.getSettingList().size() == 0)
			return R.error("settingList为空");
		if (entity.getSettingList() != null && entity.getSettingList().size() > 0) {
			for (AddedSettingEntity setting : entity.getSettingList()) {
				if (setting.getPriceList() == null || setting.getPriceList().size() == 0)
					return R.error("priceList为空");
			}
		}

		// 保存增值服务
		if (entity.getId() == null) {
			addedServiceService.save(entity);
		} else {
			addedServiceService.updateById(entity);
			// 删除原有增值服务类型设置及价格
			addedSettingService.deleteByServiceId(entity.getId());
			addedSettingPriceService.deleteByServiceId(entity.getId());
		}

		// 保存增值服务类型设置
		if (entity.getSettingList() != null && entity.getSettingList().size() > 0) {
			for (AddedSettingEntity setting : entity.getSettingList()) {
				setting.setServiceId(entity.getId());

				addedSettingService.save(setting);

				// 保存增值服务类型设置价格
				if (setting.getPriceList() != null && setting.getPriceList().size() > 0) {
					for (AddedSettingPriceEntity price : setting.getPriceList()) {
						price.setServiceId(entity.getId());
						price.setAddedSettingId(setting.getId());

						addedSettingPriceService.save(price);
					}
				}
			}
		}

		return R.ok();
	}

	/**
	 * 增值服务详情
	 */
	@GetMapping("/serviceInfo")
	public R serviceInfo(@RequestParam Integer id) {
		if (id == null)
			return R.error("id为空");

		AddedServiceEntity addedService = addedServiceService.getById(id);
		if (addedService != null) {
			List<AddedSettingEntity> settingList = addedSettingService.list(
					new QueryWrapper<AddedSettingEntity>()
							.eq("service_id", id)
			);
			if (settingList != null && settingList.size() > 0) {
				for (AddedSettingEntity entity : settingList) {
					List<AddedSettingPriceEntity> priceList = addedSettingPriceService.priceList(entity.getId());

					entity.setPriceList(priceList);
				}
			}

			addedService.setSettingList(settingList);
		}

		return R.ok().put("info", addedService);
	}

	/**
	 * 增值服务列表
	 */
	@GetMapping("/addedList")
	public R addedList(@RequestParam Map<String, Object> params){
		PageUtils page = addedServiceService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 增值服务订单
	 */
	@PostMapping("/orderList")
	public R orderList(@RequestBody ServiceQueryForm form){
		if (form.getLimit().isEmpty())
			return R.error("limit为空");
		if (form.getPage().isEmpty())
			return R.error("page为空");

		PageUtils page = userOrderService.queryPage(form);

		return R.ok().put("page", page);
	}

	/**
	 * 增值服务订单详情
	 */
	@GetMapping("/orderInfo")
	public R orderInfo(@RequestParam Integer id){
		if (id == null)
			return R.error("id为空");

		UserOrderEntity entity = userOrderService.orderInfo(id);

		return R.ok().put("info", entity);
	}

	@GetMapping("/delete")
	public R delete(@RequestParam Integer id) {
		if (id == null)
			return R.error("id为空");

		AddedServiceEntity entity = addedServiceService.getById(id);
		if (entity != null && entity.getState() == 1)
			return R.error("当前增值服务正在使用，无法删除");

		addedServiceService.removeById(id);

		return R.ok();
	}

	@GetMapping("/deleteType")
	public R deleteType(@RequestParam Integer id) {
		if (id == null)
			return R.error("id为空");

		DictionaryValueEntity entity = dictionaryValueService.getById(id);
		if (entity != null && entity.getState() == 1)
			return R.error("当前增值服务类型正在使用，无法删除");

		dictionaryValueService.removeById(id);

		return R.ok();
	}
}
